package com.catmiao.spark.sql.test

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
 * @title: Spark01_sql_test01
 * @projectName spark_study
 * @description: TODO
 * @author ChengMiao
 * @date 2024/3/20 10:44
 */
object Spark02_sql_UDF {

  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("spark_sql_01_test")

    // 创建 Spark Session对象
    val sparkSession: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
    // 涉及转换操作，需要引入转换规则
    import sparkSession.implicits._

    val df: DataFrame = sparkSession.read.json("datas/sql/user.json")


    df.createOrReplaceTempView("user")
    // 内置函数 拼接常量前缀
//    sparkSession.sql("select CONCAT('Name:',username),age from user").show
    // 自定义函数 添加前缀 注册自定义函数
    sparkSession.udf.register("prefixName","Prefix:" +_)
    sparkSession.sql("select prefixName(username),age from user").show




    // 关闭环境
    sparkSession.close()
  }

}

